﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.GYBG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class EditPM : System.Web.UI.Page
{
    /// <summary>
    /// 国研报告操作对象
    /// </summary>
    private GYBG_DAO gybgDAO = new GYBG_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        String wjm = HttpUtility.UrlDecode(Request["wjm"]);
        if (null == wjm||"".Equals(wjm))
        {
            String message = "错误：参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message="+message);
            return;
        }

        //指定文件名的国研报告
        GYBG_PM gybgPM = this.gybgDAO.GetGYBG_PMByWjm(wjm);

        if (null == gybgPM)
        {
            String message = "错误：没有找到该国研报告";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }


        //设置返回按钮链接
        String flbm=(wjm.Split('-'))[0];
        this.backHyperLink.NavigateUrl = GlobalGYBG.LISTPM_PAGE + "?catalog=" + flbm;

        //获取对应的分类信息
        GYBG_FL gybgFL = this.gybgDAO.GetGYBG_FLByFlbm(gybgPM.flbm);

        //获取栏目列表
        List<String> list = this.gybgDAO.GetLMByFL(gybgFL.flmc);
        this.lmDropDownList.Items.Clear();
        if (null != list)
        {
            foreach (String temp in list)
            {
                this.lmDropDownList.Items.Add(temp);
                if (temp.Equals(gybgPM.lm))
                {
                    this.lmDropDownList.SelectedValue = temp;
                }
            }
        }

        //分类编码
        this.flbmTextBox.Text = gybgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = gybgFL.flmc;
        //索引编码
        this.sybmTextBox.Text = gybgPM.sybm;
        //栏目
        //this.lmTextBox.Text = gybgPM.lm;
        //DOC文件名
        this.docHyperLink.Text = gybgPM.dwjm;
        this.docHyperLink.NavigateUrl = GlobalResource.VIEW_PAGE+"?fileName="+gybgPM.dwjm;
        //pdf文件名
        this.pdfHyperLink.Text = gybgPM.wjm;
        this.pdfHyperLink.NavigateUrl = GlobalResource.VIEW_PAGE + "?fileName=" + gybgPM.wjm;
        //标题
        this.btTextBox.Text = gybgPM.bt;
        //作者
        this.zzTextBox.Text = gybgPM.zz;
        //作者单位
        this.zzdwTextBox.Text = gybgPM.zzdw;
        //年份
        this.nfTextBox.Text = gybgPM.nf.ToString();
        //报告日期
        this.bgrqTextBox.Text = gybgPM.bgrq.ToString();
        //主题词
        this.ztcTextBox.Text = gybgPM.ztc;
        //来源
        this.lyTextBox.Text = gybgPM.ly;
        //文摘
        this.wzTextBox.Text = gybgPM.wz;
        //更新日期
        this.gxrqTextBox.Text = gybgPM.gxrq.ToString();
        //登录人
        this.dlrTextBox.Text = "";
        //页数
        this.ysTextBox.Text = gybgPM.ys;

        this.SetPreAndNextPM();

    }

    /// <summary>
    /// 获取用户填写的国研报告篇名信息
    /// </summary>
    /// <returns></returns>
    private GYBG_PM GetGYBGPM()
    {
        GYBG_PM gybgPM = new GYBG_PM();
        //索引编码
        gybgPM.sybm = this.sybmTextBox.Text;
        //分类编码
        gybgPM.flbm = this.flbmTextBox.Text;
        //文件名
        gybgPM.wjm = this.pdfHyperLink.Text;
        //Doc文件名
        gybgPM.dwjm = this.docHyperLink.Text;
        //标题
        gybgPM.bt = this.btTextBox.Text.Trim();
        //报告日期
        gybgPM.bgrq = DateTime.Parse(this.bgrqTextBox.Text);
        //栏目
        gybgPM.lm = this.lmDropDownList.SelectedItem.Text;
        //来源
        gybgPM.ly = this.lyTextBox.Text.Trim();
        //年份
        gybgPM.nf = Int32.Parse(this.nfTextBox.Text);
        //文摘
        gybgPM.wz = this.wzTextBox.Text;
        //主题词
        gybgPM.ztc = this.ztcTextBox.Text;
        //更新日期
        gybgPM.gxrq = DateTime.Now;
        //作者
        gybgPM.zz = this.zzTextBox.Text;
        //作者单位
        gybgPM.zzdw = this.zzdwTextBox.Text;
        //页数
        gybgPM.ys = this.ysTextBox.Text;

        return gybgPM;

    }

   

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateBGRQ())
        {
            return;
        }
        if (!this.ValidateBT())
        {
            return;
        }



        GYBG_PM gybgPM = this.GetGYBGPM();
        //验证上传文件
        if (this.ValidateYWJMFileUpload())
        {
            //上传文件
            if (null!=this.ywjmFileUpload&&!"".Equals(this.ywjmFileUpload.FileName)&&OperationFile.Upload(this.ywjmFileUpload, gybgPM.dwjm))
            {
                //删除PDF文件
                OperationFile.DeleteFile(gybgPM.wjm);
                //将doc文档转化为PDF文档
                GlobalMethod.ConvertDocToPDF(gybgPM.dwjm, gybgPM.wjm);
            }
        }



        if (this.gybgDAO.UpdateGYBG_PM(gybgPM,((User)Session["login"]).name))
        {
            Response.Write("<Script>alert('更新国研报告篇名信息成功')</Script>");
            this.NextPM();
        }
        else
        {
            Response.Write("<Script>alert('更新国研报告篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证报告日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBGRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.bgrqTextBox.Text = this.bgrqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.bgrqTextBox.Text.Trim());
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：报告日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证标题
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBT()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if (null == this.btTextBox.Text || "".Equals(this.btTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：标题不能为空";
            flag = false;
        }
        return flag;
    }
    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text.Trim());
    }
    protected void zzdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzdwTextBox.Text = GlobalMethod.Semicolon(this.zzdwTextBox.Text.Trim());
    }
    protected void ztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.ztcTextBox.Text = GlobalMethod.Semicolon(this.ztcTextBox.Text);
    }

    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        String pre = this.GetDirectionPM(-1);
        if (null != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalGYBG.EDITPM_PAGE + "?wjm=" + pre;
        }

        //下一篇
        String next = this.GetDirectionPM(1);
        if (null != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalGYBG.EDITPM_PAGE + "?wjm=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        String wjm = this.GetDirectionPM(1);
        if (null == wjm)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalGYBG.EDITPM_PAGE + "?wjm=" + wjm);
        }
    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private String GetDirectionPM(int direction)
    {
        String result = null;
        //获取文件名的后四位流水号，并在其基础上加1
        int endIndex = this.pdfHyperLink.Text.LastIndexOf(".");

        String phString = this.pdfHyperLink.Text.Substring(endIndex - 4, 4);

        int ph = 0;

        try
        {
            ph = Int32.Parse(phString);
        }
        catch (Exception)
        {
        }

        //当前篇号+1或-1
        if (direction > 0)
        {
            ph++;
        }
        else
        {
            ph--;
        }

        phString = ph.ToString();
        //长度为1
        if (phString.Length == 1)
        {
            phString = "000" + phString;
        }
        else if (phString.Length == 2)
        {
            phString = "00" + phString;
        }
        else if (phString.Length == 3)
        {
            phString = "0" + phString;
        }

        result = this.sybmTextBox.Text + "-" + phString+".PDF";
        GYBG_PM gybgPM = this.gybgDAO.GetGYBG_PMByWjm(result);
        if (null == gybgPM)
        {
            return null;
        }
        return result;
    }

    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }


    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            //this.errorLabel.Text = "错误：请选择上传的文件";
            flag = true;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".DOC", true, null))
            {
                this.errorLabel.Text = "错误：请选择DOC类型文件";
                flag = false;
            }
            else
            {
                //文件大小不能超过10M
                if (this.ywjmFileUpload.FileBytes.Length >= 10 * 1024 * 1024)
                {
                    this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                    return false;
                }
            }
        }
        return flag;
    }
}